import { RouteRecordRaw } from "vue-router";
import { PracticeItem } from "./GenerateMenu";

const newRoutes = (item: PracticeItem): Array<RouteRecordRaw> => {
  const routes: Array<RouteRecordRaw> = [];
  if (item.children.length == 0 && item.path !== undefined) {
    const path = item.path.replace("/src/views", "").replace(".vue", "");
    const newRoute: RouteRecordRaw = {
      path: path, // 新的动态路由
      name: path.replaceAll("/", ""),
      component: () => import(/* @vite-ignore */ item.path),
      meta: {
        title: item.title,
        icon: item.icon,
        markdown: item.markdown,
        KeepAlive: true
      }
    };
    routes.push(newRoute);
  } else {
    item.children.forEach(async child => {
      routes.push.apply(routes, newRoutes(child));
    });
  }
  return routes;
};

export default newRoutes;
